package com.aistar.task;

import com.aistar.mapper.DynamicCollectMapper;
import com.aistar.mapper.DynamicPraiseMapper;
import com.aistar.pojo.DynamicCollect;
import com.aistar.pojo.DynamicCollectExample;
import com.aistar.pojo.DynamicPraise;
import com.aistar.pojo.DynamicPraiseExample;
import com.aistar.util.MessageUtil;
import com.aistar.util.RedisMessage;
import com.aistar.util.RedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling   // 2.开启定时任务
public class DynamicRedisTask {
    private final DynamicPraiseMapper dynamicPraiseMapper;
    private final DynamicCollectMapper dynamicCollectMapper;
    private Log log = LogFactory.getLog(DynamicRedisTask.class);

    @Autowired
    public DynamicRedisTask(DynamicPraiseMapper dynamicPraiseMapper,
                            DynamicCollectMapper dynamicCollectMapper) {
        this.dynamicPraiseMapper = dynamicPraiseMapper;
        this.dynamicCollectMapper = dynamicCollectMapper;
    }

    @Scheduled(cron = "5/5 * * * * ?") // 每5s运行一次
//    @Scheduled(cron = "0 5/5 * * * ?") // 每5min运行一次
    private void runPraise() {
        int count = 0;
        // 将redis数据持久化到数据库中
        Map<Object, Object> map = RedisUtil.hgetAll(RedisMessage.DYNAMIC_PRAISE_KEY);
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            String[] keys = ((String) entry.getKey()).split(RedisMessage.SEPARATOR);
            Integer customerId = Integer.valueOf(keys[0]);
            Integer dynamicId = Integer.valueOf(keys[1]);

            String[] values = ((String) entry.getValue()).split(RedisMessage.SEPARATOR);
            int value = Integer.valueOf(values[0]);
            Date praiseTime = new Date(Long.valueOf(values[1]));

            DynamicPraiseExample example = new DynamicPraiseExample();
            example.createCriteria()
                    .andDynamicIdEqualTo(dynamicId)
                    .andCustomerIdEqualTo(customerId);
            List<DynamicPraise> dynamicPraiseList = dynamicPraiseMapper.selectByExample(example);
            DynamicPraise dynamicPraise;
            if (dynamicPraiseList != null && dynamicPraiseList.size() > 0) { // 数据库中有数据
                if (dynamicPraiseList.get(0).getStatus() != value) {
                    dynamicPraise = dynamicPraiseList.get(0);
                    dynamicPraise.setStatus(value);
                    dynamicPraise.setPraiseTime(praiseTime);
                    count += dynamicPraiseMapper.updateByPrimaryKeySelective(dynamicPraise);
                }
            } else if (MessageUtil.STATUS_FAIL != value) { // 数据库中没有数据，需要新增
                dynamicPraise = new DynamicPraise();
                dynamicPraise.setCustomerId(customerId);
                dynamicPraise.setDynamicId(dynamicId);
                dynamicPraise.setPraiseTime(praiseTime);
                count += dynamicPraiseMapper.insertSelective(dynamicPraise);
            }
            RedisUtil.hdel(RedisMessage.DYNAMIC_PRAISE_KEY, (String) entry.getKey());
        }
        if (count != 0)
            log.info("redis dynamicPraise change successful rows: " + count);
    }

    @Scheduled(cron = "5/5 * * * * ?") // 每5s运行一次
//    @Scheduled(cron = "0 5/5 * * * ?") // 每5min运行一次
    private void runCollect() {
        int count = 0;
        // 将redis数据持久化到数据库中
        Map<Object, Object> map = RedisUtil.hgetAll(RedisMessage.DYNAMIC_COLLECT_KEY);
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            String[] keys = ((String) entry.getKey()).split(RedisMessage.SEPARATOR);
            Integer customerId = Integer.valueOf(keys[0]);
            Integer dynamicId = Integer.valueOf(keys[1]);

            String[] values = ((String) entry.getValue()).split(RedisMessage.SEPARATOR);
            int value = Integer.valueOf(values[0]);
            Date praiseTime = new Date(Long.valueOf(values[1]));

            DynamicCollectExample example = new DynamicCollectExample();
            example.createCriteria()
                    .andDynamicIdEqualTo(dynamicId)
                    .andCustomerIdEqualTo(customerId);
            List<DynamicCollect> dynamicCollectList = dynamicCollectMapper.selectByExample(example);
            DynamicCollect dynamicCollect;
            if (dynamicCollectList != null && dynamicCollectList.size() > 0) { // 数据库中有数据
                if (dynamicCollectList.get(0).getStatus() != value) {
                    dynamicCollect = dynamicCollectList.get(0);
                    dynamicCollect.setStatus(value);
                    dynamicCollect.setDynamicCollectTime(praiseTime);
                    count += dynamicCollectMapper.updateByPrimaryKeySelective(dynamicCollect);
                }
            } else if (MessageUtil.STATUS_FAIL != value) { // 数据库中没有数据，需要新增
                dynamicCollect = new DynamicCollect();
                dynamicCollect.setCustomerId(customerId);
                dynamicCollect.setDynamicId(dynamicId);
                dynamicCollect.setDynamicCollectTime(praiseTime);
                count += dynamicCollectMapper.insertSelective(dynamicCollect);
            }
            RedisUtil.hdel(RedisMessage.DYNAMIC_COLLECT_KEY, (String) entry.getKey());
        }
        if (count != 0)
            log.info("redis dynamicCollect change successful rows: " + count);
    }
}
